package com.czxy.changgou4.controller;

import com.czxy.changgou4.gif.AnimatedGifEncoder;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 桐叔
 * @email liangtong@itcast.cn
 */
@Controller
@RequestMapping("/verifycode")
public class VerifyCodeGifController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private Random random = new Random();

    @RequestMapping
    public void verifycode(String username, HttpServletResponse httpServletResponse) throws IOException {

        //2 输出随机数据
        // 2.1 生成随机数
        String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder();
        for(int i = 1 ; i <= 4 ; i ++){
            int len = random.nextInt(VERIFY_CODES.length());
            String str = VERIFY_CODES.substring(len,len+1);
            sb.append(str);
        }
        String randomStr = sb.toString();

        // 2.2 缓存数据
        String key = "login" + username ;
        stringRedisTemplate.opsForValue().set( key , randomStr , 1 , TimeUnit.HOURS);

        // 2.3 生成对应图片，及1.5过程

        // 生成gif动态图片
        // 1.1  工具类
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        // 1.2 输出的位置，浏览器
        e.start(httpServletResponse.getOutputStream());
        // 1.3 每张图片的间隔时间，1000毫秒
        e.setDelay(1000);   // 1 frame per sec
        // 1.4 是否重复,0表示无限循环
        e.setRepeat(0);
        // 1.5 添加图片
        for (int i = 0; i < 5; i++) {
            e.addFrame(getImage(randomStr));
        }
        // 1.6 完成
        e.finish();
    }

    private BufferedImage getImage(String randomStr) {
        int IMG_WIDTH = 72;
        int IMG_HEIGTH = 27;
        //创建图片
        BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGTH, BufferedImage.TYPE_INT_RGB);
        //画板
        Graphics g = image.getGraphics();
        //填充背景
        g.setColor(Color.WHITE);
        g.fillRect(1,1,IMG_WIDTH-2,IMG_HEIGTH-2);

        g.setFont(new Font("楷体", Font.BOLD,25));

        //写字
        for(int i = 0 ; i < randomStr.length() ; i ++){
            //随机颜色
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));

            String str = randomStr.substring(i,i+1);
            // 文字 位置
            g.drawString(str, IMG_WIDTH / 6 * (i + 1) , 22 + random.nextInt(3) );
        }

        // 生成随机干扰线
        for (int i = 0; i < 10; i++) {
            //随机颜色
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            int x = random.nextInt(IMG_WIDTH - 1);
            int y = random.nextInt(IMG_HEIGTH - 1);
            int x1 = random.nextInt(12) + 1;
            int y1 = random.nextInt(6) + 1;
            g.drawLine(x, y, x - x1, y - y1);
        }

        return image;
    }

}
